//
//  HRSelectIconViewController.m
//  Hear
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRSelectIconViewController.h"
#import "HRSelectIconView.h"
#import "HRSelectIconBackView.h"
#import "HRIconButton.h"

@interface HRSelectIconViewController ()
@property (nonatomic, strong)HRIconButton *iconImageView;
@property (nonatomic, strong)HRSelectIconView *selectIconView;
@property (nonatomic, strong)HRSelectIconBackView *selectIconBackView;
@property (nonatomic, copy)NSString *selectIconName;
@property (nonatomic, copy)NSString *selectIconColor;

@end

@implementation HRSelectIconViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"选择头像";

//    UIButton *leftButton = [UIButton buttonWithType:0];
//    leftButton.size = CGSizeMake(20, 20);
//    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
//    [leftButton addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton *rightButton = [UIButton buttonWithType:0];
    [rightButton setTitle:@"确定" forState:0];
    rightButton.titleLabel.font = CustomUIFont(13);
    rightButton.size = CGSizeMake(40, 40);
    [rightButton addTarget:self action:@selector(clickEnter) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    [self setupView];
}

- (void)clickEnter
{
    if (!self.selectIconName.length)
    {
        [MBProgressHUD showMessage:@"请选择一个头像" finishBlock:nil];
        return;
    }
    if (!self.selectIconColor.length)
    {
        [MBProgressHUD showMessage:@"请选择一个头像背景色" finishBlock:nil];
        return;
    }
    if (self.selectIconBlock)
    {
        self.selectIconBlock(self.selectIconName,self.selectIconColor);
    }
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)setupView
{
    HRIconButton *iconImageView = [HRIconButton new];
    iconImageView.contentMode = UIViewContentModeScaleAspectFill;
    iconImageView.clipsToBounds = YES;
    [self.view addSubview:(self.iconImageView = iconImageView)];
    iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    iconImageView.layer.borderWidth = 1;
    iconImageView.layer.cornerRadius = 65;
    iconImageView.backgroundColor = [UIColor lightGrayColor];

    WeakSelf
    self.selectIconView = [[HRSelectIconView alloc] initWithFrame:CGRectZero andSelectType:self.selectStr];
    [self.view addSubview:self.selectIconView];
    self.selectIconView.selectIconBlock = ^(NSString *selectIconName){
        [weakSelf.iconImageView setImage:[UIImage imageNamed:selectIconName] forState:0];
        weakSelf.selectIconName = selectIconName;
    };
    
    self.selectIconBackView = [HRSelectIconBackView new];
    [self.view addSubview:self.selectIconBackView];
    self.selectIconBackView.selectIconBackColorBlock = ^(NSString *selectIconColor){
        weakSelf.iconImageView.backgroundColor = [UIColor colorWithHexString:selectIconColor];
        weakSelf.selectIconColor = selectIconColor;
    };
    
    
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.top.equalTo(weakSelf.view).offset(80);
        make.size.mas_equalTo(CGSizeMake(130, 130));
    }];
    
    [self.selectIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.iconImageView.mas_bottom).offset(30);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(kFullWidth, 170));
    }];
    
    [self.selectIconBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.selectIconView.mas_bottom).offset(30);
        make.centerX.equalTo(weakSelf.view);
        make.size.equalTo(weakSelf.selectIconView);
    }];
    
    
}


- (void)clickBack
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
